"""
游戏主程序入口点
模块化重构后的主文件，负责整合各个模块并启动游戏
"""

import pygame
from game import Game
from ui import UI
from map_editor import MapEditor
from ai_ui import AIUI

class Main:
    """主程序类，负责整合各个模块"""
    
    def __init__(self):
        """初始化主程序"""
        pygame.init()
        self.game = Game()
        self.map_editor = MapEditor(self.game)
        self.ui = UI(self.game, self.map_editor)
        self.ai_ui = AIUI(self.game, self)  # 传入Main类的引用

    def run(self):
        """运行主程序"""
        while True:
            # 游戏状态机
            if self.game.state == 'main_menu':
                self.run_main_menu()
            elif self.game.state == 'map_selection':
                self.run_map_selection()
            elif self.game.state == 'game':
                self.run_game()
            elif self.game.state == 'editor':
                self.run_editor()
            elif self.game.state == 'ai_menu':
                self.ai_ui.run_ai_menu()
            elif self.game.state == 'model_selection':
                self.ai_ui.run_model_selection()
            elif self.game.state == 'training':
                self.ai_ui.run_training()
            elif self.game.state == 'ai_vs_player':
                self.ai_ui.run_ai_vs_player()
            elif self.game.state == 'save_menu':
                self.run_save_menu()
            elif self.game.state == 'input_map_name':
                self.run_input_map_name()

    def run_main_menu(self):
        """运行主菜单"""
        self.game.playing = True
        while self.game.playing and self.game.state == 'main_menu':
            self.game.dt = self.game.clock.tick(self.game.FPS) / 1000.0
            self.handle_events()
            self.draw_main_menu()

    def run_map_selection(self):
        """运行地图选择菜单"""
        self.game.playing = True
        while self.game.playing and self.game.state == 'map_selection':
            self.game.dt = self.game.clock.tick(self.game.FPS) / 1000.0
            self.handle_events()
            self.draw_map_selection()

    def run_game(self):
        """运行游戏"""
        self.game.playing = True
        # 重置玩家按键状态，避免状态切换时的粘滞键问题
        if hasattr(self.game, 'player') and self.game.player:
            self.game.player.reset_keys()
        while self.game.playing and self.game.state == 'game':
            self.game.dt = self.game.clock.tick(self.game.FPS) / 1000.0
            self.handle_events()
            self.game.update()
            # 更新AI状态
            self.ai_ui.update_in_game(self.game.clock.get_time())
            self.draw_game()
            # 绘制AI状态UI
            self.ai_ui.draw_in_game_ui()
    
    def game_events(self):
        """处理游戏事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.game.quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.game.state = 'main_menu'
                # 处理玩家按键
                if hasattr(self.game, 'player') and self.game.player and self.game.player.alive():
                    self.game.player.handle_key_down(event.key)
            elif event.type == pygame.KEYUP:
                # 处理玩家按键释放
                if hasattr(self.game, 'player') and self.game.player and self.game.player.alive():
                    self.game.player.handle_key_up(event.key)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:  # 左键
                    if hasattr(self.game, 'player') and self.game.player and self.game.player.alive():
                        self.game.player.shoot(pygame.mouse.get_pos())
                elif event.button == 3:  # 右键
                    if hasattr(self.game, 'player') and self.game.player and self.game.player.alive():
                        self.game.handle_player_move_click(pygame.mouse.get_pos())

    def run_editor(self):
        """运行地图编辑器"""
        self.game.playing = True
        while self.game.playing and self.game.state == 'editor':
            self.game.dt = self.game.clock.tick(self.game.FPS) / 1000.0
            self.handle_events()
            self.draw_editor()

    def run_save_menu(self):
        """运行保存菜单"""
        self.game.playing = True
        while self.game.playing and self.game.state == 'save_menu':
            self.game.dt = self.game.clock.tick(self.game.FPS) / 1000.0
            self.handle_events()
            self.draw_save_menu()

    def run_input_map_name(self):
        """运行输入地图名称界面"""
        self.game.playing = True
        while self.game.playing and self.game.state == 'input_map_name':
            self.game.dt = self.game.clock.tick(self.game.FPS) / 1000.0
            self.handle_events()
            self.draw_input_map_name()

    def handle_events(self):
        """处理事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.game.quit()
            
            # 全局事件
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    # 在游戏或编辑器中按ESC返回主菜单
                    if self.game.state in ['game', 'editor']:
                        self.game.state = 'main_menu'

            # 状态特定的事件
            if self.game.state == 'main_menu':
                self.ui.main_menu_events(event)
            elif self.game.state == 'map_selection':
                self.ui.map_selection_events(event)
            elif self.game.state == 'game':
                self.game.game_events(event)
            elif self.game.state == 'editor':
                self.map_editor.editor_events(event)
            elif self.game.state == 'save_menu':
                self.ui.save_menu_events(event)
            elif self.game.state == 'ai_menu':
                self.ai_ui.ai_menu_events(event)
            elif self.game.state == 'model_selection':
                self.ai_ui.model_selection_events(event)
            elif self.game.state == 'training':
                self.ai_ui.training_events(event)
            elif self.game.state == 'ai_vs_player':
                self.ai_ui.ai_vs_player_events(event)
            elif self.game.state == 'input_map_name':
                self.ui.input_map_name_events(event)

    def draw_main_menu(self):
        """绘制主菜单"""
        self.game.screen.fill(pygame.Color(40, 40, 40))  # DARKGREY
        self.ui.draw_main_menu()
        pygame.display.flip()

    def draw_map_selection(self):
        """绘制地图选择菜单"""
        self.game.screen.fill(pygame.Color(40, 40, 40))  # DARKGREY
        self.ui.draw_map_selection_menu()
        pygame.display.flip()

    def draw_game(self):
        """绘制游戏界面"""
        self.game.draw()
        pygame.display.flip()

    def draw_editor(self):
        """绘制地图编辑器"""
        self.game.screen.fill(pygame.Color(40, 40, 40))  # DARKGREY
        self.map_editor.draw_editor_ui()
        pygame.display.flip()

    def draw_save_menu(self):
        """绘制保存菜单"""
        self.game.screen.fill(pygame.Color(40, 40, 40))  # DARKGREY
        self.ui.draw_save_menu()
        pygame.display.flip()

    def draw_input_map_name(self):
        """绘制输入地图名称界面"""
        self.game.screen.fill(pygame.Color(40, 40, 40))  # DARKGREY
        self.ui.draw_input_map_name()
        pygame.display.flip()




if __name__ == '__main__':
    """程序入口点"""
    main = Main()
    main.run()